import Mock from 'mockjs';
import qs from 'query-string';
import type { GetParams } from '@/types/global';
import setupMock, { successResponseWrap } from './setup-mock';

const { Random } = Mock;

const data = Mock.mock({
  'list|55': [
    {
      'id|8': /[A-Z][a-z][-][0-9]/, // 随机生成8位id
      'avatar':
        'https://img2.baidu.com/it/u=3422222222,2834555555&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      'title': /韩国艺人刘在石确诊新冠/, // 随机生成标题
      'readCount|2-3': /[0-9]/, // 随机生成2-3之间的数字
      'createdTime': Random.datetime() // 随机生成时间
    }
  ]
});

setupMock({
  setup() {
    Mock.mock(new RegExp('/api/message/getList'), (params: GetParams) => {
      const { current = 1, pageSize = 10 } = qs.parseUrl(params.url).query;
      const p = current as number;
      const ps = pageSize as number;
      return successResponseWrap({
        list: data.list.slice((p - 1) * ps, p * ps),
        total: 55
      });
    });
  }
});
